<?php
class MemberAction extends BaseAction {
	public function member() {
		//防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$User = M('User'); 
		
		//搜索不同类别的会员
		$flag=$_GET['flag'];
		if($flag==2) $condition['user_type'] = 2;
		else if($flag==3) $condition['user_type'] = 3;
		else if($flag==4) $condition['veri_user'] = 0;
		$condition['available'] = 1;
		
		$list = $User->where($condition)->select(); 
		$count = count($list);
    	// 导入分页类
   	 	import("ORG.Util.Page");
    	// 实例化分页类
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	
    	// 分页显示输出
    	$page = $p->show();
		$list = $User->order('user_id DESC')->limit($p->firstRow.','.$p->listRows)->where($condition)->select(); 
		
		//按照生日计算年龄
		function getAge($birth) {
			list($by,$bm,$bd)=explode('-',$birth);
			$age=date('Y')-$by;
			return $age;
		}
		
		for($i=0;$i<count($list);$i++) {
			$list[$i]["user_age"] = getAge($list[$i]["user_birthday"]);
			if($list[$i]["veri_user"]==0) $list[$i]["veri_user_cn"]="待审核";
			else if($list[$i]["veri_user"]==1) $list[$i]["veri_user_cn"]="已通过";
			else $list[$i]["veri_user_cn"]="禁用";
			if($list[$i]["user_type"]==0) $list[$i]["user_type_cn"]="注册会员";
			else if($list[$i]["user_type"]==1) $list[$i]["user_type_cn"]="线下会员";
			else if($list[$i]["user_type"]==2) $list[$i]["user_type_cn"]="VIP会员";
			else $list[$i]["user_type_cn"]="贵宾会员";
		}
		
		if($flag==1) {
			$this->assign('navi',"会员列表");
			$this->assign('title',"注册会员列表");
		}
		else if($flag==2) {
			$this->assign('navi',"搜索会员");
			$this->assign('title',"搜索会员列表");
		}
		else if($flag==3){
			$this->assign('navi',"贵宾会员");
			$this->assign('title',"贵宾会员列表");
		}
		else {
			$this->assign('navi',"会员资料审核");
			$this->assign('title',"资料审核列表");
		}
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function search() {
		//防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Opt = M('Option');
		$opt_condition['opt_flag'] = "age";
		$agelist = $Opt->order('opt_id')->where($opt_condition)->select();
		
		$this->assign("agelist",$agelist);
		$this->display();
	}
	
	public function result() {
		//防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		function getAge($birth) {
			list($by,$bm,$bd)=explode('-',$birth);
			$age=date('Y')-$by;
			return $age;
		}
		
		function getBirth($age,$flag) {
			$birth=date("Y")-$age;
			if($flag==1) $birth = $birth."-01-01";
			else $birth = $birth."-12-31";
			return $birth;
		}
		
		$id = $_POST["search_id"];
		$nickname = $_POST["search_nickname"];
		$name = $_POST["search_name"];
		$tel = $_POST["search_tel"];
		$sex = $_POST["search_sex"];
		$age_min = $_POST["search_age_1"];
		$age_max = $_POST["search_age_2"];
		$type = $_POST["search_type"];
		$state = $_POST["search_state"];
		$fuzzy = $_POST["search_fuzzy"];
		
		if($id != null) $condition['user_id'] = $id;
		if($nickname != null) {
			if($fuzzy!="on") $condition['user_nickname'] = $nickname;
			else $condition['user_nickname'] = array(array('like','%'.$nickname.'%')); 
		}
		if($name != null) {
			if($fuzzy!="on") $condition['user_truename'] = $name;
			else $condition['user_truename'] = array(array('like','%'.$name.'%'));
		}
		if($tel != null) $condition['user_mobile'] = $tel;
		if($sex == "male") $condition['user_sex'] = "男士";
		else if($sex == "female") $condition['user_sex'] = "女士";
		if($age_min != null) {
			if($age_max == null) $condition['user_birthday'] = array(array('elt',getBirth($age_min,0)));
			else $condition['user_birthday'] = array(array('elt',getBirth($age_min,0)),array('egt',getBirth($age_max,1)));
		}
		else if($age_max != null) $condition['user_birthday'] = array(array('egt',getBirth($age_max,1)));
		if($type == "0") $condition['user_type'] = $type;
		else if($type == "1") $condition['user_type'] = $type;
		else if($type == "2") $condition['user_type'] = $type;
		else if($type == "3") $condition['user_type'] = $type;
		if($state == "0") $condition['veri_user'] = $state;
		else if($state == "1") $condition['veri_user'] = $state;
		else if($state == "2") $condition['veri_user'] = $state;

		$User = M('User'); 
		$condition['available'] = 1;
		$list = $User->where($condition)->select(); 
		$count = count($list);
   	 	import("ORG.Util.Page");
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	$page = $p->show();
		$list = $User->order('user_id DESC')->limit($p->firstRow.','.$p->listRows)->where($condition)->select(); 
		
		for($i=0;$i<count($list);$i++) {
			$list[$i]["user_age"] = getAge($list[$i]["user_birthday"]);
			if($list[$i]["veri_user"]==0) $list[$i]["veri_user_cn"]="待审核";
			else if($list[$i]["veri_user"]==1) $list[$i]["veri_user_cn"]="已通过";
			else $list[$i]["veri_user_cn"]="禁用";
			if($list[$i]["user_type"]==0) $list[$i]["user_type_cn"]="注册会员";
			else if($list[$i]["user_type"]==1) $list[$i]["user_type_cn"]="线下会员";
			else if($list[$i]["user_type"]==2) $list[$i]["user_type_cn"]="VIP会员";
			else $list[$i]["user_type_cn"]="贵宾会员";
		}
		$this->assign('navi',"搜索会员");
		$this->assign('title',"搜索结果");
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->display('main');
	}
	
	public function delete() {
		//防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$User = M('User');
		$condition['user_id'] = $_GET['id'];
		$result = $User->where($condition)->delete();
		if($result)
			echo "<script language=\"javascript\">alert('删除成功!');window.location.href='../../member';</script>";
		else 
			$this->error ( '数据写入错误！请重试...' );
	}
}